-- Tags: no-fasttest, long, no-asan, no-msan
-- msan: too slow

SET enable_json_type = 1;
set session_timezone = 'UTC';
set output_format_native_write_json_as_string = 0;
set max_threads=1;

drop table if exists test;

{% for engine in ['Memory',
                  'MergeTree order by tuple() settings min_rows_for_wide_part=100000000, min_bytes_for_wide_part=1000000000',
                  'MergeTree order by tuple() settings min_rows_for_wide_part=1, min_bytes_for_wide_part=1'] -%}

select '{{ engine }}';
create table test (json JSON(max_dynamic_paths=4, k1 UInt32, k2 String)) engine= {{ engine }};
insert into test format JSONAsObject
{"k1" : 1, "k2" : "2020-01-01", "k3" : 31, "k4" : [1, 2, 3], "k5" : "str1",               "k7" : "2020-01-01",              "k9" : 91}
{"k1" : 2, "k2" : "2020-01-02",            "k4" : "Hello"  ,                "k6" : false,                         "k9" : 92}
{"k1" : 3, "k2" : "2020-01-03", "k3" : 32,                   "k5" : 42    ,               "k7" : 73                        }
{"k1" : 4, "k2" : "2020-01-04",            "k4" : false    , "k5" : "str1", "k6" : true,            "k8" : 84.84, "k9" : 94}

select 'Increase max_dynamic_paths to 5';
alter table test modify column json JSON(max_dynamic_paths=5, k1 UInt32, k2 String);
select json from test;
select 'Dynamic paths:';
select distinct arrayJoin(JSONDynamicPaths(json)) from test order by all;
select 'Shared data paths:';
select distinct arrayJoin(JSONSharedDataPaths(json)) from test order by all;
select json.k0, json.k1, json.k2, json.k3, json.k4, json.k5, json.k6, json.k7, json.k8, json.k9 from test;

select 'Decrease max_dynamic_paths to 3, max_dynamic_types to 1';
alter table test modify column json JSON(max_dynamic_paths=3, max_dynamic_types=1, k1 UInt32, k2 String);
select json from test;
select 'Dynamic paths:';
select distinct arrayJoin(JSONDynamicPaths(json)) from test order by all;
select 'Shared data paths:';
select distinct arrayJoin(JSONSharedDataPaths(json)) from test order by all;
select json.k0, json.k1, json.k2, json.k3, json.k4, json.k5, json.k6, json.k7, json.k8, json.k9 from test;

select 'Change typed paths';
alter table test modify column json JSON(max_dynamic_paths=3, max_dynamic_types=1, k2 Date, k3 UInt32, k6 Bool, k0 UInt32);
select json from test;
select 'Dynamic paths:';
select distinct arrayJoin(JSONDynamicPaths(json)) from test order by all;
select 'Shared data paths:';
select distinct arrayJoin(JSONSharedDataPaths(json)) from test order by all;
select json.k0, json.k1, json.k2, json.k3, json.k4, json.k5, json.k6, json.k7, json.k8, json.k9 from test;

select 'Add new skip rules';
alter table test modify column json JSON(max_dynamic_paths=3, max_dynamic_types=1, k2 Date, k3 UInt32, k6 Bool, k0 UInt32, SKIP k5, SKIP k8);
select json from test;
select 'Dynamic paths:';
select distinct arrayJoin(JSONDynamicPaths(json)) from test order by all;
select 'Shared data paths:';
select distinct arrayJoin(JSONSharedDataPaths(json)) from test order by all;
select json.k0, json.k1, json.k2, json.k3, json.k4, json.k5, json.k6, json.k7, json.k8, json.k9 from test;

select 'Decrease max_dynamic_paths and max_dynamic_types to 0';
alter table test modify column json JSON(max_dynamic_paths=0, max_dynamic_types=0, k2 Date, k3 UInt32, k6 Bool, k0 UInt32, SKIP k5, SKIP k8);
select json from test;
select 'Dynamic paths:';
select distinct arrayJoin(JSONDynamicPaths(json)) from test order by all;
select 'Shared data paths:';
select distinct arrayJoin(JSONSharedDataPaths(json)) from test order by all;
select json.k0, json.k1, json.k2, json.k3, json.k4, json.k5, json.k6, json.k7, json.k8, json.k9 from test;

drop table test;

create table test (id UInt64, json JSON(max_dynamic_paths=4)) engine={{ engine }};
insert into test select number, multiIf(number < 1000, '{"k2" : 42}', number < 3000, '{"k3" : 42}', number < 6000, '{"k4" : 42}', number < 10000, '{"k1" : 42}', '{"k1" : 42, "k2" : 42, "k3" : 42, "k4" : 42}') from numbers(15000);

select 'max_dynamic_paths=3';
alter table test modify column json JSON(max_dynamic_paths=3);
select 'All paths:';
select distinct arrayJoin(JSONAllPaths(json)) from test order by all;

drop table test;
create table test (id UInt64, json JSON(max_dynamic_paths=4)) engine={{ engine }};
insert into test select number, multiIf(number < 1000, '{"k2" : 42}', number < 3000, '{"k3" : 42}', number < 6000, '{"k4" : 42}', number < 10000, '{"k1" : 42}', '{"k1" : 42, "k2" : 42, "k3" : 42, "k4" : 42}') from numbers(15000);

select 'max_dynamic_paths=2';
alter table test modify column json JSON(max_dynamic_paths=2);
select 'All paths:';
select distinct arrayJoin(JSONAllPaths(json)) from test order by all;

drop table test;
create table test (id UInt64, json JSON(max_dynamic_paths=4)) engine={{ engine }};
insert into test select number, multiIf(number < 1000, '{"k2" : 42}', number < 3000, '{"k3" : 42}', number < 6000, '{"k4" : 42}', number < 10000, '{"k1" : 42}', '{"k1" : 42, "k2" : 42, "k3" : 42, "k4" : 42}') from numbers(15000);

select 'max_dynamic_paths=1';
alter table test modify column json JSON(max_dynamic_paths=1);
select 'All paths:';
select distinct arrayJoin(JSONAllPaths(json)) from test order by all;

drop table test;
create table test (id UInt64, json JSON(max_dynamic_paths=4)) engine={{ engine }};
insert into test select number, multiIf(number < 1000, '{"k2" : 42}', number < 3000, '{"k3" : 42}', number < 6000, '{"k4" : 42}', number < 10000, '{"k1" : 42}', '{"k1" : 42, "k2" : 42, "k3" : 42, "k4" : 42}') from numbers(15000);

select 'max_dynamic_paths=0';
alter table test modify column json JSON(max_dynamic_paths=0);
select 'All paths:';
select distinct arrayJoin(JSONAllPaths(json)) from test order by all;

drop table test;

{% endfor -%}
